// pages/xunpan-reply/index.js
import http from "../../../../class/Utils/Http"
import tips from "../../../../class/Utils/Tips"
var app = getApp();
Page({
    data: {
        mingpian_s: 0,
        talklist: []
        // talklist: [
        //     {val: "您的产品我们需要大量进货，价格方面是否有优惠？", time: '2018-05-02 16:42:30', type: '1', ntype: 1},
        //     {
        //         val: "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=127731595,1631492217&fm=27&gp=0.jpg",
        //         time: '2018-05-02 16:28:30',
        //         type: '2',
        //         ntype: 2
        //     },
        //     {val: "我试试文字", time: '2018-05-02 16:42:30', type: '1', ntype: 1},
        //     {
        //         val: "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2003834304,4134858953&fm=27&gp=0.jpg",
        //         time: '2018-05-02 16:28:30',
        //         type: '2',
        //         ntype: 1
        //     },
        // ]
    },
    /*
    询盘 思路  将  auserid  buserid  uuuserid  传输到 后台
     */
    onLoad: function (options) {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            user: user
        })
        var auserid = options.auserid
        var buserid = options.buserid
        var uuuserid = options.uuuserid
        var userid = auserid==user.userid?buserid:auserid
        /*
         名片人
         */
      wx.showLoading({
        title: '加载中',
      })

        http.req("/Mingpian/info", {userid: userid}, function (res) {
            console.log(res)
            that.setData({
                mp_info: res
            })
        })
        http.req("/Mingpian/talk_list?auserid=" + auserid + "&buserid=" + buserid + "&uuuserid=" + uuuserid, "", function (ressss) {
            console.log(ressss)
            if (ressss.code == 1) {
                var list = ressss.data
                // for(var i=0;i<list.length;i++){
                //     if(list[i].userid == auserid){
                //         list[i].touxiang = that.data.mp_info.pic
                //     }
                // }
                that.setData({
                    talklist: list
                })
                setTimeout(function () {
                    that.ObtainpageHeight()
                }, 500) //延迟时间 这里是0.5秒
            }
            that.setData({
                mid: ressss.mid
            })
          wx.hideLoading()

        })
        // var intervalId = setInterval(function () {
        //     http.req("/Mingpian/talk_list?auserid="+auserid+"&buserid="+buserid+"&uuuserid="+uuuserid,"",function (ressss) {
        //         console.log(ressss)
        //         if(ressss.code==1){
        //             var list = ressss.data
        //             // for(var i=0;i<list.length;i++){
        //             //     if(list[i].userid == auserid){
        //             //         list[i].touxiang = that.data.mp_info.pic
        //             //     }
        //             // }
        //             that.setData({
        //                 talklist:list
        //             })
        //
        //         }
        //         that.setData({
        //             mid:ressss.mid
        //         })
        //
        //     })
        // }, 1000);
        // setTimeout(function () {
        //     that.ObtainpageHeight()
        // }, 500) //延迟时间 这里是0.5秒


    },
    onReady: function () {

    },
    onShow: function () {
        var that = this;
        that.ObtainpageHeight();
    },
    //复制微信号
    copyweixin: function (e) {
        var self = this;
        wx.setClipboardData({
            data: self.data.mp_info.weixin,
            success: function (res) {
                // self.setData({copyTip:true}),
                // wx.showModal({
                //     title: '提示',
                //     content: '复制成功',
                //     success: function (res) {
                //         if (res.confirm) {
                //             console.log('确定')
                //         } else if (res.cancel) {
                //             console.log('取消')
                //         }
                //     }
                // })
            }
        });
    },
    //添加到联系人
    phoneNumTap: function () {
        var that = this;
        // 提示呼叫号码还是将号码添加到手机通讯录
        wx.showActionSheet({
            itemList: ['呼叫', '添加联系人'],
            success: function (res) {
                if (res.tapIndex === 0) {
                    // 呼叫号码
                    wx.makePhoneCall({
                        phoneNumber: that.data.mp_info.shouji,
                    })
                } else if (res.tapIndex == 1) {
                    // 添加到手机通讯录
                    wx.addPhoneContact({
                        //lastName: that.data.lastName,
                        photoFilePath: that.data.mp_info.pic,//头像
                        firstName: that.data.mp_info.xingming,//联系人姓名
                        organization: that.data.mp_info.gongsi,//公司名称
                        title: that.data.mp_info.zhiwei,//公司职位
                        mobilePhoneNumber: that.data.mp_info.shouji,//联系人手机号
                        weChatNumber: that.data.mp_info.weixin,//微信号
                        email: that.data.mp_info.youxiang,//电子邮箱
                        workAddressCity: that.data.mp_info.dizhi,//工作城市
                    })
                }
            }
        })
    },
    //点击图片放大
    previewImage: function (e) {
        console.log(e)
        var that = this;
        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: [current] // 需要预览的图片http链接列表
        })
    },
    //查看联系人询盘信息
    contactinfo: function () {
        var that = this
        var continfoa = that.data.continfo
        if (continfoa == 0) {
            that.setData({
                continfo: 1
            })
        } else {
            that.setData({
                continfo: 0
            })
        }
    },

    //获取 input 内容
    Obtaininputtext: function (e) {
        var that = this
        that.setData({
            inputtext: e.detail.value
        })
        //console.log(e.detail.value)
    },
    //输入文字发送
    uploadtext: function () {
        var that = this

        var user = wx.getStorageSync('user');
        //获取输入的文字转化为数组
        var inputtextinfo = that.data.inputtext
      if (!inputtextinfo){
        tips.error('不能发送空信息')
        return
      }

        //获取发送文字的时间转化为数组
        var that = this
        // 时间
        var newDate = new Date()
        // var timeStamp = newDate.getTime(); //获取时间戳
        var date = newDate.setMinutes(newDate.getMinutes());
        newDate = new Date(date);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var day = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if (month < 10) {
            month = '0' + month;
        }
        ;
        if (day < 10) {
            day = '0' + day;
        }
        ;
        if (h < 10) {
            h = '0' + h;
        }
        ;
        if (m < 10) {
            m = '0' + m;
        }
        ;
        if (s < 10) {
            s = '0' + s;
        }
        ;
        var timesz = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
        //转化数组
        var text_arr = {
            val: inputtextinfo,
            type: 1,
            ntype: 1,
            time: timesz,
            touxiang: that.data.user.wx_img
        }

        var newtalklist = that.data.talklist
      newtalklist.push(text_arr)
        that.setData({
            talklist: newtalklist
        })

        that.setData({
            inputtextzi: ''
        })

        that.ObtainpageHeight()

        /*
         添加文字留言
         */


        var formdata = {
            userid: user.userid,
            mid: that.data.mid,
            val: inputtextinfo,
            type: 1
        }
        http.req("/Mingpian/add_liuyan", formdata)
        that.setData({
          inputtext:''
        })
    },
    //选择图片发送
    uploadimg: function () {
        var that = this;

        var user = wx.getStorageSync('user');
        //获取发送文字的时间转化为数组
        var that = this
        // 时间
        var newDate = new Date()
        // var timeStamp = newDate.getTime(); //获取时间戳
        var date = newDate.setMinutes(newDate.getMinutes());
        newDate = new Date(date);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var day = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if (month < 10) {
            month = '0' + month;
        }
        ;
        if (day < 10) {
            day = '0' + day;
        }
        ;
        if (h < 10) {
            h = '0' + h;
        }
        ;
        if (m < 10) {
            m = '0' + m;
        }
        ;
        if (s < 10) {
            s = '0' + s;
        }
        ;
        var timesz = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
        var formdata = {
            userid: user.userid,
            mid: that.data.mid,
            type: 2,
            touxiang: that.data.user.wx_img
        }
        var rootDocment = app.httpurl.url
        wx.chooseImage({
            count: 1, // 选择图片的数量，最多9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有

            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: rootDocment + '/Mingpian/add_liuyan',
                    filePath: tempFilePaths[0],
                    name: 'val', //文件对应的参数名字(key)
                    formData: formdata,  //其它的表单信息
                    success: function (suc) {
                        console.log(suc)
                    },
                })

                //转化为数组
                var img_arr = {
                    val: tempFilePaths,
                    type: 2,
                    ntype: 1,
                    time: timesz,
                    touxiang: that.data.user.wx_img
                }
                var newtalklist = that.data.talklist
                newtalklist.push(img_arr)

                that.setData({
                    talklist: newtalklist
                })

                that.ObtainpageHeight()
            }
        })
    },
    //获取页面高度到底部
    ObtainpageHeight: function () {
        //滚动到页面最底部
        wx.createSelectorQuery().selectAll('.el-xunpan-textul').boundingClientRect(function (rects) {
            rects.forEach(function (rect) {
                wx.pageScrollTo({
                    scrollTop: rect.height
                })
            })
        }).exec()
    },
    //获取当前时间
    dataTime: function () {
        var that = this
        // 时间
        var newDate = new Date()
        // var timeStamp = newDate.getTime(); //获取时间戳
        var date = newDate.setMinutes(newDate.getMinutes());
        newDate = new Date(date);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var day = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if (month < 10) {
            month = '0' + month;
        }
        ;
        if (day < 10) {
            day = '0' + day;
        }
        ;
        if (h < 10) {
            h = '0' + h;
        }
        ;
        if (m < 10) {
            m = '0' + m;
        }
        ;
        if (s < 10) {
            s = '0' + s;
        }
        ;
        var time = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
        that.setData({
            ttime: time,
        })
        console.log(time)
    }


})